//
//  TecniciDettaglioInterventoViewController.m
//  InterventixApp
//
//  Created by Diego Falcinelli on 05/06/13.
//
//

#import "TecniciDettaglioInterventoViewController.h"
#import "Syncronize.h"
#import "UtenteCD.h"
#import "CoreDataManager.h"


@interface TecniciDettaglioInterventoViewController ()

@end

@implementation TecniciDettaglioInterventoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated{
    self.techs = [self.coreDataManager getUtenti:@"TECNICO"];
    self.admins = [self.coreDataManager getUtenti:@"AMMINISTRATORE"];
    [self.table reloadData];
}

-(IBAction) indietro{
	[[self navigationController] popViewControllerAnimated:YES];
}


// TABELLA

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0){
        return self.techs.count;
    }
    if(section == 1){
        return self.admins.count;
    }
    return 0;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if(section == 0){
        return @"Tecnici";
    }
    if(section == 1){
        return @"Amministratori";
    }
	return @"";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0){
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        [cell setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:1]];
        
        UtenteCD *u = [self.techs objectAtIndex:indexPath.row];
        cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",u.cognome, u.nome];
        if([self existUserInToTecnici:u]){
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }else{
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        return cell;
    }
    if(indexPath.section == 1){
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        [cell setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:1]];
        
        UtenteCD *u = [self.admins objectAtIndex:indexPath.row];
        cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",u.cognome, u.nome];
        if([self existUserInToTecnici:u]){
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }else{
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        return cell;
    }
    return nil;
}

-(BOOL) existUserInToTecnici:(UtenteCD *) u33{
    for(UtenteCD *uu in self.dett_intervento.tecnici){
        if([uu.idutente intValue] == [u33.idutente intValue]){
            return true;
        }
    }
    return false;
}

// CellHeight
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 50;
}

// CLICK
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0){
        NSString *name = [self.techs objectAtIndex:indexPath.row];
        self.nameSelect = name;
        
        
        UtenteCD *u = [self.techs objectAtIndex:indexPath.row];
        
        if([self existUserInToTecnici:u]){
            [self.dett_intervento removeTecniciObject:u];
        }else{
            [self.dett_intervento addTecniciObject:u];
        }
        [self.table reloadData];
    }
    if(indexPath.section == 1){
        NSString *name = [self.admins objectAtIndex:indexPath.row];
        self.nameSelect = name;
        
        
        UtenteCD *u = [self.admins objectAtIndex:indexPath.row];
        
        if([self existUserInToTecnici:u]){
            [self.dett_intervento removeTecniciObject:u];
        }else{
            [self.dett_intervento addTecniciObject:u];
        }
        [self.table reloadData];
    }
}

@end
